<?php

class Categories_Model extends Model {

	function Categories_Model()
	{
		parent::Model();
	}
	
	//todo: optimize
	function add_category($catname) {
		$data = array(
			'title' => $catname
		);

		$this->db->insert('categories', $data); 
		
		//get the new id
		$this->db->where('title', $catname);
		$query = $this->db->get('categories');
		
		$results = $query->result();
		if (!empty($results)) {
			return $results[0]->id;
		} else {
			return FALSE;
		}
	}
	
	
	//todo: optimize
	function add($category) {
		//$category is $_POST array
		// $category['title'], ['description'], ['parentid']
		
		//insert it into the categories table
		$data['title'] = $category['title'];
		$data['description'] = $category['description'];

		$this->db->insert('categories', $data);
		
		//we need the id of the category we just inserted
		$this->db->where('title', $data['title']);
		$this->db->select('id');
		$query = $this->db->get('categories');
		
		$result = $query->result();
		$catid = $result[0]->id;
		
		//insert into category_parents
		$data2['catid'] = $catid;
		$data2['parentid'] = $category['parentid'];
		
		$this->db->insert('category_parents', $data2);
		
		
		return $catid;
		
		
	}
	
	function getevent($catid) {
		$this->db->where('catid', $catid);
		$query = $this->db->get('events');
		$results = $query->result();
		
		return $results[0];
	}
	
	function eventadd($event) {
		
		//add it to the category and category_parents tables
		$catid = $this->add($event);
		
		//now the events table
		$data = '';
		$data['start_date'] = $event["startdate"];
		$data['end_date'] = $event["enddate"];
		$data['catid'] = $catid;
		$data['discount_type'] = $event["eventtype"];
		if ($event["eventtype"] == "percentage")
			$data['discount'] = $event["percentagediscount"];
		else
			$data['discount'] = $event["dollardiscount"];
			
		$this->db->insert('events', $data);
		
		return $catid;
		
	}
	
	function eventupdate($event) {
		//categories table
		$data['title'] = $event['title'];
		$data['description'] = $event['description'];
		$this->db->where('id', $event['id']);
		$this->db->update('categories', $data);
		
		//events table
		$data2['start_date'] = $event['startdate'];
		$data2['end_date'] = $event['enddate'];
		$this->db->where('catid', $event['id']);
		$data2['discount_type'] = $event['eventtype'];
		if ($event['eventtype'] == 'percentage')
			$data2['discount'] = $event['percentagediscount'];
		else
			$data2['discount'] = $event['dollardiscount'];
			
		$this->db->update('events', $data2);
		return TRUE;
	}
	
	//todo: optimize
	function update($category) {
		//$category is $_POST array
		// $category['title'], ['description'], ['parentid']
		
		//update the categories table
		$data['title'] = $category['title'];
		$data['description'] = $category['description'];
		
		$this->db->where('id', $category['id']);

		$this->db->update('categories', $data);
		
		//update category_parents
		$data2['catid'] = $category['id'];
		$data2['parentid'] = $category['parentid'];
		
		$this->db->where('catid', $category['id']);
		
		$this->db->update('category_parents', $data2);
		
		return TRUE;
		
		
	}
	
	
	//todo: optimize
	function delete_category($catid) {
		
		//get the parentid of this category
		$this->db->where('catid', $catid);
		$this->db->select('parentid');
		$query = $this->db->get('category_parents');
		$results = $query->result();
		$parentid = $results[0]->parentid;
		$data = '';
		$data['parentid'] = $parentid;
		
		//categories table
		$this->db->delete('categories', array('id' => $catid));
		
		//item2cat table
		$this->db->where('catid', $catid);
		$this->db->delete('item2cat');
		
		//move children to have parent of $catid's parent
		$this->db->where('parentid', $catid);
		$this->db->update('category_parents', $data);
		
		//delete from category_parents
		$this->db->where('catid', $catid);
		$this->db->delete('category_parents');
		
		return TRUE;
	}
	
	//todo: optimize
	function delete_event($catid) {
		
		//delete from the events table
		$this->db->where('catid', $catid);
		$this->db->delete('events');
		
		return TRUE;
	}
	
	//todo: optimize
	function get_my_categories($id) {
		$this->db->where('itemid', $id);
		$this->db->select('catid');
		$query = $this->db->get('item2cat');
		$results = $query->result();
		if ($query->num_rows() > 0) {
			foreach ($results as $key => $result ) {
				$res[$key] = $result->catid;
			}
			return $res;
		} else { return array(NULL); }
	}
	
	//todo: optimize
	function get_catname($ids) {
	
		
		if (is_array($ids)) {
			foreach($ids as $id) {
				$this->db->or_where('id', $id);
			}
			$this->db->select('title');
			$query = $this->db->get('categories');
			$results = $query->result();
			
			$c = 0;
			$cname = '';
			foreach ($results as $result) {
				$cname[$c] = $result->title;
				$c++;
			}
			return $cname;
		} else {
			if ($ids != '') {
				
				$query = $this->db->query('SELECT title FROM categories WHERE id = ' . $ids );

				$result = $query->result();
				$cname = $result[0]->title;
				return $cname;
			} else {
				return FALSE;
			}	
		}
	}
	
	function get_catid($catname) {
		$this->db->where('title', $catname);
		$this->db->select('id');
		$query = $this->db->get('categories');
		$results = $query->result();
		if (isset($results[0]->id)) {
			return $results[0]->id;
		} else {
			return FALSE;
		}
	}
	
	function get_item_count($catid) {
		$this->db->where('catid', $catid);
		return $this->db->count_all_results('item2cat');
	}
	
	//todo: optimize
	function get_category($itemid) {
		$query = $this->db->query('SELECT catid FROM item2cat WHERE itemid =' . $itemid);
		$result = $query->result();
		$c=0;
		
		foreach ($result as $row) {
			$c++;
			$catid[$c] = $row->catid;
		}
		
		if ($c > 0) {
			return $catid;
		} else {
			return '';
		}
	}
	
	function getEndDate($catid) {
		$this->db->where('catid', $catid);
		$this->db->select('end_date');
		$query = $this->db->get('events');
		
		$results = $query->result();
		if ($query->num_rows != 0) {
			return $results[0]->end_date;
		} else {
			return FALSE;
		}
	}
	
	//todo: optimize
	function get_category_title_and_description($catid) {
		$query = $this->db->query('SELECT * FROM categories WHERE id =' . $catid);
		$results = $query->result();
		
		$result->title = $results[0]->title;
		$result->description = $results[0]->description;
		
		return $result;
		
	}
	
	function number_categories() {
		//no events
		$this->db->where('parentid', 99);
		$minus = $this->db->count_all_results('category_parents');
		
		//not the events category
		$this->db->where('id !=', 99);
		$result = $this->db->count_all_results('categories');
		
		return $result - $minus;
		
	}
	
	function number_events() {
		$this->db->where('parentid', 99);
		return $this->db->count_all_results('category_parents');
	}
	
	
	
	//todo: optimize
	function get_categories() {
		$query = $this->db->query('SELECT * FROM categories');
		$result = $query->result();
		$categories = '';
		foreach($result as $c => $row) {
			$categories[$c]->catid = $row->id;
			$categories[$c]->catname = $row->title;
			$categories[$c]->description = $row->description;
			$categories[$c]->count = $this->get_item_count($categories[$c]->catid);
			$categories[$c]->parentid = $this->getParent($categories[$c]->catid)->id;
		}
		
		return $categories;
	}
	
	
	
	function get_hierarchy() {
		
		$categories = $this->get_categories();
		
		foreach ($categories as $c => $category) {
			$categories[$c]->sortname = $categories[$c]->catname;
			$categories[$c]->tabs = '';
			//get the parent
			$parent->id = $category->catid;
			while ($parent->id != -1) {
				$parent = $this->getParent($parent->id);
				
				if ($parent->id != -1) {
					$categories[$c]->sortname = $parent->title . " --- " . $categories[$c]->sortname;
					$categories[$c]->tabs = '&nbsp;&nbsp;&nbsp;' . $categories[$c]->tabs;
				}
			}
			
		}
		
		//sort it alphabetically by catname
		usort($categories, "me_sort");
		return $categories;
		
		
		/* $categories = '';
		
		$topLevel = $this->getTopLevel();
		foreach ($topLevel as $parent) {
			$catid = $parent->catid;
			
			//get the category name
			$categories->$catid->title = $this->get_catname($catid);
			$categories->$catid->catid = $catid;
			
			//get the children of this top level category
			$children = $this->getChildren($catid);
			
			foreach ($children as $child) {
				
			}
			
		}
		
		return $categories; */
	}
	
	//todo: optimize
	function getTopLevel() {
		$this->db->where('parentid', '-1');
		$this->db->select('catid');
		$query = $this->db->get('category_parents');
		return $query->result();
	}
	
	function getChildren($parentid) {
		$this->db->where('parentid', $parentid);
		$this->db->select('catid');
		$query = $this->db->get('category_parents');
		$results = $query->result();
		
		//get the names
		foreach ($results as $result) {
			$this->db->or_where('id', $result->catid);
		}
		$query = $this->db->get('categories');
		$results = $query->result();
		
		//how many children does each have?
		foreach ($results as $key => $result) {
			$this->db->where('parentid', $result->id);
			$this->db->from('category_parents');
			$results[$key]->children = $this->db->count_all_results();
			if ($parentid != -1) {
				$this->db->where('id', $parentid);
				$this->db->select('title');
				$query = $this->db->get('categories');
				$res = $query->result();
				$results[$key]->parentid = $parentid;
				$results[$key]->parent = $res[0]->title;
				
				//up a level: parent of the parent
				$this->db->where('catid', $parentid);
				$this->db->select('parentid');
				$query = $this->db->get('category_parents');
				$res = $query->result();
				$results[$key]->up = $res[0]->parentid;
				
			}
		}
		
		return $results;
	}
	
	function getParent($childid) {
		$this->db->where('catid', $childid);
		$this->db->select('parentid');
		$query = $this->db->get('category_parents');
		$results = $query->result();
		
		$parent->id = $results[0]->parentid;
		
		//get the name
		if ($results[0]->parentid != -1) {
			$this->db->where('id', $results[0]->parentid);
			$query = $this->db->get('categories');
			$results = $query->result();
		
			$parent->title = $results[0]->title;
		}
		
		return $parent;
	}
	
}
?>